/**
 * Created by Administrator on 2017/8/10.
 */

// 判断背景图片是否加载成功

function onloadImg(fun) {
    var obj=new Image();
    obj.src = "assets/img/chartPag/chartPag_bg.png";
    obj.onload = function () {
        fun();
    }
}

function adaptation(px) {
    var fontSize = parseFloat($("html").css("font-size"));
    return px/16*fontSize;
}

/***获取组件**/
function getCompone(url,fun) {
    $.ajax({ url: url,dataType:"html",context: document.body,async:false,success: function(dom){
        fun(dom);
    }});
}

//首页
var Index = {
    indexDom:null,
    startUpDom:null,
    loadingDom:null,
    loginDom:null,
    platformDom:null,
    shoppingCartDom:null,
    brandDom:null,
    chartPagDom:null,
    mySwiper:null,
    init:function () {
        this.indexDom = $("#index");
        this.startUpDom = this.indexDom.find("#startUp");
        this.loadingDom = this.indexDom.find("#loading");
        this.loginDom = this.indexDom.find("#login");
        this.platformDom = this.indexDom.find("#platform");
        this.shoppingCartDom = this.indexDom.find("#shoppingCart");
        this.brandDom = this.indexDom.find("#brand");
        this.chartPagDom = this.indexDom.find("#chartPag");

        var that = this;

        setTimeout(function () {
            that.ready(function () {
                //that.loadingDom.hide();
                mySwiper = new Swiper('#homepage',{
                    direction:'horizontal',
                    iOSEdgeSwipeDetection : true,
                    iOSEdgeSwipeThreshold : 50,
                    touchRatio : 1,
                    touchMoveStopPropagation : false,
                    threshold : 200,
                    onSlideChangeEnd: function(swiper){
                        var index = swiper.activeIndex;

                        if(index==1){
                            Login.formDom.css("background-image","url(assets/img/login/2.gif)")
                        }
                        if(index==2){
                            Index.indexDom.find("img").attr('src','assets/img/bg.gif');
                        }
                        if(index==2 || index==3 || index==4){
                            $("#bottom").show();
                            recording.switchCondition = index;
                        }else {
                            $("#bottom").hide();
                        }
                    }
                });
            });
        },0);
        //添加录音功能
        recording.init();

    },
    ready:function (fun) {
        var that = this;
        getCompone("login.html",function (dom) {
            that.loginDom.html(dom);
        });
        getCompone("platform.html",function (dom) {
            that.platformDom.html(dom);
        });
        getCompone("shoppingCart.html",function (dom) {
            that.shoppingCartDom.html(dom);
        });
        getCompone("brand.html",function (dom) {
            that.brandDom.html(dom);
        });
        getCompone("chartPag.html",function (dom) {
            that.chartPagDom.html(dom);
        });
        fun()
    }
}
//登录
var Login = {
    loginDom:null,
    formDom:null,
    init:function () {
        this.loginDom = $("#login");
        this.formDom = this.loginDom.find("form");
        var loginDom = $("#login");
        var inputDom = loginDom.find("input");
        inputDom.on('touchstart',function () {
            inputDom.parent().css("background-image","url('assets/img/login/input_border.png')");
            if($(this).attr("type")=="button"){
                $(inputDom[1]).parent().css("background-image","url('assets/img/login/input-warm.png')");
                $(inputDom[1]).val('').attr("placeholder","WRONG PASSWORD").css("font-size",adaptation(25)+"px")
            }else {
                $(this).parent().css("background-image","url('assets/img/login/input_select.png')");
            }
        })
    }
}
//数据统计页面
var ChartPag = {
    chartPagDom:null,
    startYearDom:null,
    startMouthDom:null,
    startDayDom:null,
    endYearDom:null,
    endMouthDom:null,
    endDayDom:null,
    selectLiDom:null,
    selectLiBDom:null,
    optionDom:null,
    ulDom:null,
    confirmDom:null,
    barrageDom:null,
    rightBarDom:null,
    centenDom:null,
    chartBigDom:null,
    chartBigTransformDom:null,
    chartSmallDom:null,
    chartSmallTransformDom:null,
    LineDom:null,
    lineTransform:null,
    switchingUlDom:null,
    brandListUlDom:null,
    piePromptDom:null,
    startYear:2014,
    startMouth:0,
    startDay:2,
    endYear:2015,
    endMouth:0,
    endDay:1,
    //路线图对象
    line: null,
    lineData:null,
    Letter:['JAN','FEB','MAR','APR','MAY','JUN','JUL','AUG','SEP','OCT','NOV','DEC'],
    init:function () {
        this.chartPagDom = $("#chartPag");
        this.startYearDom = this.chartPagDom.find("#startYear");
        this.startMouthDom = this.chartPagDom.find("#startMouth");
        this.startDayDom = this.chartPagDom.find("#startDay");
        this.endYearDom = this.chartPagDom.find("#endYear");
        this.endMouthDom = this.chartPagDom.find("#endMouth");
        this.endDayDom = this.chartPagDom.find("#endDay");
        this.selectLiDom = this.chartPagDom.find(".select li");
        this.selectLiBDom = this.chartPagDom.find(".select li b");
        this.optionDom = this.chartPagDom.find(".option");
        this.ulDom = this.optionDom.find(".ul");
        this.confirmDom = $("#confirm");
        this.barrageDom = this.chartPagDom.find("#barrage");
        this.rightBarDom = this.chartPagDom.find("#rightBar");
        this.centenDom = this.chartPagDom.find("#centen");
        this.chartBigDom = this.chartPagDom.find("#chartBig");
        this.chartBigTransformDom = this.chartBigDom.find("#chartBigTransform")
        this.chartSmallDom = this.chartPagDom.find("#chartSmall");
        this.chartSmallTransformDom = this.chartSmallDom.find("#chartSmallTransform");
        this.LineDom = this.chartPagDom.find("#Line");
        this.lineTransform = this.LineDom.find("#lineTransform");
        this.switchingUlDom = this.LineDom.find("#switching ul");
        this.brandListUlDom = this.LineDom.find("#brandList ul");
        this.piePromptDom = this.chartBigDom.find("#piePrompt");

        var that = this;

        this.chartBigTransformDom.on('touchstart',function () {
            that.sceneSwitching(0);
            return false;
        })
        this.chartSmallTransformDom.on('touchstart',function () {
            that.sceneSwitching(1);
            return false;
        })
        this.lineTransform.on('touchstart',function () {
            that.sceneSwitching(2);
            return false;
        })
        //事件监听
        $("body").on('touchstart',function () {
            that.optionDom.slideUp("slow");
        });
        //下拉框监听
        $.each(this.selectLiBDom,function (i,o) {
            this.index = i;
            $(this).on('touchstart',function (event) {
                var index = this.index;
                $.each(that.optionDom,function (i,o) {
                    if(index===i){
                        $(this).slideToggle("slow");
                        $(this).parent().attr("class","selected");
                    }else {
                        $(this).slideUp("slow");
                        $(this).parent().attr("class","");
                    }
                });
                return false;
            });
        });
        //下拉项事件监听
        this.ulDom.on('touchstart',function (event) {
            var t = $(event.target);
            var thatParentVal = t.parent().attr("id");
            if(thatParentVal=="startYear"){
                that.startYear = parseInt(t.html());
            }else if(thatParentVal=="startMouth"){
                that.startMouth = that.Letter.indexOf(t.html());
            }else if(thatParentVal=="startDay"){
                that.startDay = parseInt(t.html());
            }else if(thatParentVal=="endYear"){
                that.endYear = parseInt(t.html());
            }else if(thatParentVal=="endMouth"){
                that.endMouth = that.Letter.indexOf(t.html());
            }else if(thatParentVal=="endDay"){
                that.endDay = parseInt(t.html());
            }
            that.setDate();
        });
        //确定按钮事件
        this.confirmDom.on('touchstart',function () {
            console.log(that.startYear+"==="+that.startMouth+"==="+that.startDay);
            that.barrageToggle();
            that.bigPie();
            return false;
        })
        //切换
        this.switchingUlDom.find("span").on('touchstart',function (event) {
            that.switchingUlDom.children("li").attr("class","");
            $(event.target).parent().attr("class","active");
            var text = $(event.target).html();
            if(text=="D"){
                that.lineData = {
                    title:{
                        text:"折线图"
                    },
                    legend: {
                        data:['海飞丝','潘婷','飘柔','沙宣','SK-II']
                    },
                    xAxis : {
                        data : ['01','02','03','04','05','06','07']
                    },
                    series : [
                        {
                            name:'邮件营销',
                            type:'line',
                            stack: '总量',
                            color:"#000",
                            areaStyle: {normal: {}},
                            data:[200, 189, 466, 300, 500, 770, 1200]
                        },
                        {
                            name:'联盟广告',
                            type:'line',
                            stack: '总量',
                            color:'#0FF',
                            areaStyle: {normal: {}},
                            data:[340, 100, 200, 600, 450, 800, 400]
                        },
                        {
                            name:'视频广告',
                            type:'line',
                            stack: '总量',
                            color:'#FF0',
                            areaStyle: {normal: {}},
                            data:[0, 432, 900, 700, 200, 1200, 1100]
                        },
                        {
                            name:'直接访问',
                            type:'line',
                            stack: '总量',
                            color:'#0F0',
                            areaStyle: {normal: {}},
                            data:[341, 457, 500, 600, 800, 400, 900]
                        },
                        {
                            name:'搜索引擎',
                            type:'line',
                            stack: '总量',
                            color:'#FFF',
                            label: {
                                normal: {
                                    show: true,
                                    position: 'top'
                                }
                            },
                            areaStyle: {normal: {}},
                            data:[100, 932, 901, 934, 1290, 1330, 1320]
                        }
                    ]
                }
                that.line.onload(that.lineData);
            }else if(text=="W"){
                that.lineData = {
                    title:{
                        text:"折线图"
                    },
                    legend: {
                        data:['海飞丝','潘婷','飘柔','沙宣','SK-II']
                    },
                    xAxis : {
                        data : ['周一','周二','周三','周四','周五','周六','周日']
                    },
                    series : [
                        {
                            name:'邮件营销',
                            type:'line',
                            stack: '总量',
                            color:"#000",
                            areaStyle: {normal: {}},
                            data:[400, 345, 100, 700, 660, 770, 1200]
                        },
                        {
                            name:'联盟广告',
                            type:'line',
                            stack: '总量',
                            color:'#0FF',
                            areaStyle: {normal: {}},
                            data:[340, 100, 500, 600, 450, 600, 400]
                        },
                        {
                            name:'视频广告',
                            type:'line',
                            stack: '总量',
                            color:'#FF0',
                            areaStyle: {normal: {}},
                            data:[0, 432, 600, 700, 900, 1200, 1100]
                        },
                        {
                            name:'直接访问',
                            type:'line',
                            stack: '总量',
                            color:'#0F0',
                            areaStyle: {normal: {}},
                            data:[200, 400, 500, 600, 800, 400, 800]
                        },
                        {
                            name:'搜索引擎',
                            type:'line',
                            stack: '总量',
                            color:'#FFF',
                            label: {
                                normal: {
                                    show: true,
                                    position: 'top'
                                }
                            },
                            areaStyle: {normal: {}},
                            data:[820, 932, 901, 934, 1290, 1330, 1320]
                        }
                    ]
                }
                that.line.onload(that.lineData);
            }else if(text=="M"){
                that.lineData = {
                    title:{
                        text:"折线图"
                    },
                    legend: {
                        data:['海飞丝','潘婷','飘柔','沙宣','SK-II']
                    },
                    xAxis : {
                        data : ['JAN','FEB','MAR','APR','MAY','JUN','JUL']
                    },
                    series : [
                        {
                            name:'邮件营销',
                            type:'line',
                            stack: '总量',
                            color:"#000",
                            areaStyle: {normal: {}},
                            data:[200, 189, 466, 300, 500, 770, 1200]
                        },
                        {
                            name:'联盟广告',
                            type:'line',
                            stack: '总量',
                            color:'#0FF',
                            areaStyle: {normal: {}},
                            data:[340, 100, 200, 600, 450, 800, 400]
                        },
                        {
                            name:'视频广告',
                            type:'line',
                            stack: '总量',
                            color:'#FF0',
                            areaStyle: {normal: {}},
                            data:[0, 432, 900, 700, 200, 1200, 1100]
                        },
                        {
                            name:'直接访问',
                            type:'line',
                            stack: '总量',
                            color:'#0F0',
                            areaStyle: {normal: {}},
                            data:[341, 457, 500, 600, 800, 400, 900]
                        },
                        {
                            name:'搜索引擎',
                            type:'line',
                            stack: '总量',
                            color:'#FFF',
                            label: {
                                normal: {
                                    show: true,
                                    position: 'top'
                                }
                            },
                            areaStyle: {normal: {}},
                            data:[100, 932, 901, 934, 1290, 1330, 1320]
                        }
                    ]
                }
                that.line.onload(that.lineData);
            }
        });
        this.brandListUlDom.on('touchstart',function (event) {
            that.brandListUlDom.children("li").attr("class","");
            $(event.target).attr("class","active");
            var text = $(event.target).html();

            var index = that.lineData.legend.data.indexOf(text);
            that.line.selectLine(that.lineData,index);
        })
        //设置起始结束时间
        this.setDate();
        //加载背景图片
        onloadImg(function () {
            that.chartPagDom.show();
        });

    },
    //实例化大饼图
    bigPie:function () {
        var data = [
            {
                id:0,
                name:"天猫",
                value:0.4,
                color:"#031e66",
                selectColor:"rgba(145,220,255,0.2)"
            },
            {
                id:1,
                name:"京东",
                value:0.6,
                color:"#00b1ff",
                selectColor:"rgba(146,221,255,0.6)"
            }
        ]
        var childData = [
            [
                {
                    id:0,
                    name:"宝洁士",
                    value:0.3,
                    color:"#006ec8",
                    selectColor:"rgba(255,255,255,0.6)1"
                },
                {
                    id:1,
                    name:"宝洁士",
                    value:0.25,
                    color:"#042171",
                    selectColor:"rgba(255,255,255,0.1)"
                },
                {
                    id:2,
                    name:"宝洁士",
                    value:0.15,
                    color:"#00b1ff",
                    select:"rgba(255,255,255,0.4)"
                },
                {
                    id:3,
                    name:"宝洁士",
                    value:0.3,
                    color:"#0042c8",
                    select:"rgba(255,255,255,0.2)"
                }
            ],
            [
                {
                    id:0,
                    name:"宝洁士",
                    value:0.6,
                    color:"#006ec8",
                    selectColor:"rgba(255,255,255,0.6)"
                },
                    {
                        id:1,
                        name:"宝洁士",
                        value:0.25,
                        color:"#042171",
                        selectColor:"rgba(255,255,255,0.1)"
                    },
                    {
                        id:2,
                        name:"宝洁士",
                        value:0.15,
                        color:"#00b1ff",
                        select:"rgba(255,255,255,0.4)"
                    }
                ]
        ]
        this.lineData = {
            title:{
                text:"折线图"
            },
            legend: {
                data:['海飞丝','潘婷','飘柔','沙宣','SK-II']
            },
            xAxis : {
                data : ['01','02','03','04','05','06','07']
            },
            series : [
                {
                    name:'邮件营销',
                    type:'line',
                    stack: '总量',
                    color:"#000",
                    areaStyle: {normal: {}},
                    data:[200, 189, 466, 300, 500, 770, 1200]
                },
                {
                    name:'联盟广告',
                    type:'line',
                    stack: '总量',
                    color:'#0FF',
                    areaStyle: {normal: {}},
                    data:[340, 100, 200, 600, 450, 800, 400]
                },
                {
                    name:'视频广告',
                    type:'line',
                    stack: '总量',
                    color:'#FF0',
                    areaStyle: {normal: {}},
                    data:[0, 432, 900, 700, 200, 1200, 1100]
                },
                {
                    name:'直接访问',
                    type:'line',
                    stack: '总量',
                    color:'#0F0',
                    areaStyle: {normal: {}},
                    data:[341, 457, 500, 600, 800, 400, 900]
                },
                {
                    name:'搜索引擎',
                    type:'line',
                    stack: '总量',
                    color:'#FFF',
                    label: {
                        normal: {
                            show: true,
                            position: 'top'
                        }
                    },
                    areaStyle: {normal: {}},
                    data:[100, 932, 901, 934, 1290, 1330, 1320]
                }
            ]
        }
        var html = "";
        var previousDeg = 0;
        var deg = 0;
        for (var i=0;i<data.length;i++){

            if(i==0){
                previousDeg = -45;
            }else {
                previousDeg += data[i-1].value*360;
            }

            if(previousDeg>90){

            }

            console.log(previousDeg);

            html+="<div style='transform: rotate("+previousDeg+"deg)' class='piePromptItem'>"+
            "<div class='piePromptItem-line'>"+
            "<div style='transform: rotate("+(180-previousDeg)+"deg)' class='piePromptItem-border'>"+
            "<span>"+data[i].name+"</span>"+
            "<p>"+data[i].value*100+"%</p>"+
            "</div></div>"+
            "</div>"
        }
        this.piePromptDom.html(html);
        this.chartBigDom.show();
        this.line = new drawLineChat(this.lineData);
        new BigPie(data,childData,this.lineData,this.line);
    },
    sceneSwitching:function (i) {
        //获取页面元素属性
        var centenWidth = this.centenDom.width()*2;
        var centenHeight = this.centenDom.height()*2;
        var charBigHeight = adaptation(350)*2;
        var lineWidth = adaptation(350)*2;
        var chartSmallHeight = adaptation(350)*2;
        if(i==0){
            this.chartBigTransformDom.attr("class","active");
            this.chartBigDom.find(".title").css("transform","translate3d("+charBigHeight/2+"px,"+(charBigHeight+adaptation(20))+"px,0)")
            this.chartSmallTransformDom.attr("class","").css("transform","translate3d(0,0,0) scale3d(1,1,1)");
            this.chartSmallDom.find(".title").css("transform","translate3d(0,0,0)");
            this.lineTransform.attr("class","").css("transform","translate3d(0,0,0) scale3d(1,1,1)");
            this.LineDom.find(".title").css("transform","translate3d(0,0,0)");
            this.LineDom.find("ul").css("display","none");
        }else if(i==1){
            this.chartBigTransformDom.attr("class","");
            this.chartBigDom.find(".title").css("transform","translate3d(0,0,0)");
            this.chartSmallTransformDom.attr("class","active").css({"transform-origin":chartSmallHeight+"px 0","transform":"translate3d(0,0,0) scale3d(2,2,2)"});
            this.chartSmallDom.find(".title").css("transform","translate3d(-"+chartSmallHeight/2+"px,"+(chartSmallHeight+adaptation(30))+"px,0)")
            this.lineTransform.attr("class","").css("transform","translate3d(-"+(centenWidth-lineWidth)+"px,0,0) scale3d(1,1,1)");
            this.LineDom.find(".title").css("transform","translate3d(-"+(centenWidth-lineWidth)+"px,0,0)");
            this.LineDom.find("ul").css("display","none");
        }else {
            this.chartBigTransformDom.attr("class","");
            this.chartBigDom.find(".title").css("transform","translate3d(0,0,0)")
            this.chartSmallTransformDom.attr("class","").css("transform","translate3d(-"+(centenWidth-lineWidth)+"px,"+(centenHeight-chartSmallHeight)+"px,0) scale3d(1,1,1)");
            this.chartSmallDom.find(".title").css("transform","translate3d(-"+(centenWidth-chartSmallHeight)+"px,"+(centenHeight-chartSmallHeight)+"px,0)")
            this.lineTransform.attr("class","active").css({"transform-origin":lineWidth+"px "+lineWidth+"px","transform":"translate3d(-"+adaptation(40)+"px,-"+adaptation(50)+"px,0) scale3d(2,2,2)"});
            this.LineDom.find(".title").css("transform","translate3d(-"+lineWidth/2+"px,-"+(adaptation(30))+"px,0)");
            this.LineDom.find("ul").css("display","block");
        }
    },
    addSildHTML:function (dom,arr,index) {
        var html = "";
        for (var i=0;i<arr.length;i++){
            if(arr[i]==index){
                html+="<div class='li selected-item'>"+arr[i]+"</div>";
            }else {
                html+="<div class='li'>"+arr[i]+"</div>";
            }
        }
        dom.html(html);
    },
    //弹幕开启关闭
    barrageToggle:function () {
      if(this.barrageDom.css("display")=="none"){
          this.rightBarDom.css("transform","translateX(0)");
      }else {
          this.rightBarDom.css("transform","translateX(100%)");
      }
      this.barrageDom.fadeToggle("slow");
    },
    setDate:function () {
        $(".startYearValue").html(this.startYear);
        $(".startMouthValue").html(this.Letter[this.startMouth]);
        $(".startDayValue").html(this.startDay<10?'0'+this.startDay:this.startDay);
        $(".endYearValue").html(this.endYear);
        $(".endMouthValue").html(this.Letter[this.endMouth]);
        $(".endDayValue").html(this.endDay<10?'0'+this.endDay:this.endDay);
        //设置起始时间
        this.addSildHTML(this.startYearDom,getTime(2006,0,'year'),this.startYear);
        this.addSildHTML(this.startMouthDom,getTime(0,11,'mouth'),this.Letter[this.startMouth]);
        var maxDay = getDaysInMonth(this.startYear,this.startMouth);
        this.addSildHTML(this.startDayDom,getTime(1,maxDay,'day'),this.startDay);
        //设置结束时间
        this.addSildHTML(this.endYearDom,getTime(2006,0,'year'),this.endYear);
        this.addSildHTML(this.endMouthDom,getTime(0,11,'mouth'),this.Letter[this.endMouth]);
        var maxDay = getDaysInMonth(this.endYear,this.endMouth);
        this.addSildHTML(this.endDayDom,getTime(1,maxDay,'day'),this.endDay);
    }
}
//购物车页面
var ShoppingCart = {
    mapDom:null,
    city_inputDom:null,
    swiperWrapperDom:null,
    cityData:null,
    mySwiper:null,
    url1:"url(assets/img/shoppingCart/city-big.png)",
    url2:"url(assets/img/shoppingCart/city-select.png)",
    init:function () {
        //初始化Dom树
        var shoppingCartDom = $("#shoppingCart");
        this.mapDom = shoppingCartDom.find(".map");
        this.city_inputDom = shoppingCartDom.find(".city_input");
        //添加城市节点
        this.cityData = [
            {
                id:1,
                provinceName:'BEIJING',
                cityName:'BEI<br>JING',
                cityName_CN:'北京',
                data:true,
                box:true,
                coordinate:{x:adaptation(585),y:adaptation(305)}
            },
            {
                id:2,
                provinceName:'SHANGHAI',
                cityName:'SHANG<br>HAI',
                cityName_CN:'上海',
                data:true,
                box:true,
                coordinate:{x:adaptation(682),y:adaptation(460)}
            },
            {
                id:3,
                provinceName:'GUANGGONG',
                cityName:'GUANG<br>ZHOU',
                cityName_CN:'广州',
                data:true,
                box:false,
                coordinate:{x:adaptation(568),y:adaptation(620)}
            },
            {
                id:4,
                provinceName:'SICHUAN',
                cityName:'CHENG<br>DU',
                cityName_CN:'成都',
                data:true,
                box:false,
                coordinate:{x:adaptation(421),y:adaptation(490)}
            },
            {
                id:5,
                provinceName:'HUBEI',
                cityName:'WU<br>HAN',
                cityName_CN:'武汉',
                data:false,
                box:false,
                coordinate:{x:adaptation(570),y:adaptation(483)}
            },
            {
                id:6,
                provinceName:'TIANJIN',
                cityName:'TIAN<br>JIN',
                cityName_CN:'天津',
                data:false,
                box:true,
                coordinate:{x:adaptation(600),y:adaptation(324)}
            },
            {
                id:7,
                provinceName:'SHANDONG',
                cityName:'JI<br>NAN',
                cityName_CN:'济南',
                data:false,
                box:false,
                coordinate:{x:adaptation(620),y:adaptation(375)}
            },
            {
                id:8,
                provinceName:'LIAONING',
                cityName:'SHEN<br>YANG',
                cityName_CN:'沈阳',
                data:false,
                box:false,
                coordinate:{x:adaptation(675),y:adaptation(265)}
            },
            {
                id:9,
                provinceName:'ZHEJIANG',
                cityName:'JIN<br>HUA',
                cityName_CN:'金华',
                data:true,
                box:true,
                coordinate:{x:adaptation(650),y:adaptation(510)}
            },
            {
                id:10,
                provinceName:'JIANGSU',
                cityName:'SU<br>ZHOU',
                cityName_CN:'苏州',
                data:false,
                box:true,
                coordinate:{x:adaptation(665),y:adaptation(450)}
            },
            {
                id:11,
                provinceName:'ZHEJIANG',
                cityName:'HANG<br>ZHOU',
                cityName_CN:'杭州',
                data:true ,
                box:true,
                coordinate:{x:adaptation(658),y:adaptation(490)}
            },
            {
                id:12,
                provinceName:'JIANGSU',
                cityName:'NAN<br>JING',
                cityName_CN:'南京',
                data:true,
                box:true,
                coordinate:{x:adaptation(645),y:adaptation(450)}
            },
            {
                id:13,
                provinceName:'FUJIAN',
                cityName:'JIN<br>JIANG',
                cityName_CN:'晋江',
                data:false,
                box:false,
                coordinate:{x:adaptation(645),y:adaptation(572)}
            },
            {
                id:14,
                provinceName:'SHANXI',
                cityName:"XI<br>&#39AN",
                cityName_CN:'西安',
                data:false,
                box:false,
                coordinate:{x:adaptation(480),y:adaptation(425)}
            },
            {
                id:15,
                provinceName:'GUANGDONG',
                cityName:'HUI<br>YANG',
                cityName_CN:'惠阳',
                data:false,
                box:false,
                coordinate:{x:adaptation(590),y:adaptation(610)}
            },
            {
                id:16,
                provinceName:'HENAN',
                cityName:'ZHENG<br>ZHOU',
                cityName_CN:'郑州',
                data:false,
                box:false,
                coordinate:{x:adaptation(558),y:adaptation(415)}
            }
        ];
        this.addCity(this.cityData);
    },
    addCity:function (arr) {
        var html = "";
        for(var i=0;i<arr.length;i++){
            var url = "";
            var zIndex = "";
            var x = "";
            var y = "";
            var w = "";

            if(arr[i].data){
                zIndex = '9999';
                url = 'assets/img/shoppingCart/city-big.png';
                x = arr[i].coordinate.x+adaptation(5);
                y = arr[i].coordinate.y+adaptation(5);
                w = adaptation(40);
            }else {
                zIndex = '0';
                url = 'assets/img/shoppingCart/city-small.png';
                x = arr[i].coordinate.x+adaptation(10);
                y = arr[i].coordinate.y+adaptation(10);
                w = adaptation(30);
            }

            html+="<div ontouchstart='ShoppingCart.selectCity(event,"+JSON.stringify(arr[i])+")' style='left: "+x+"px;top: "+y+"px;background-image: url("+url+");z-index: "+zIndex+";width: "+w+"px;height: "+w+"px' ></div>";
        }
        this.mapDom.html(html);
    },
    selectCity:function (event,obj) {
        if(obj.data){
            if(obj.box){
                this.addCityList(obj);
            }else {
                $(event.target).css("background-image",ShoppingCart.url2);
                this.addCar($(event.target),$("<p>"+obj.cityName+"</p>"),event,obj.id);
            }
        }else {
            alert("无数据");
            //无数据
        }
    },
    addCityList:function (obj) {
        var cityData = this.cityData;
        var html = "<h3 class='highlight'>"+obj.provinceName+"</h3>"+
        "<div class='city_input_centen'>"+
            "<div class='swiper-container' id='citySwiper'>"+
            "<div class='swiper-wrapper'>";
        var count = 0;
        var index = 0;
        for(var i=0;i<cityData.length;i++){
            if(cityData[i].data && cityData[i].provinceName==obj.provinceName ){
                html+="<div class='swiper-slide' id='"+cityData[i].id+"' >"+cityData[i].cityName.replace("<br>","")+"</div>";
                if(cityData[i].id==obj.id){
                    index = count;
                }
                ++count;
            }
        }
        for (var j=count;j<5;j++){
            html+="<div class='swiper-slide'></div>";
        }
        html+="</div>"+
            "</div>"+
            "</div>"+
            "<h3 ontouchstart='ShoppingCart.add()' class='btn highlight'>ADD TO LIST</h3>"+
            "</div>";
        this.city_inputDom.html(html);
        this.city_inputDom.css({"animation":"scaleAinm 0.5s forwards"});
        this.swiperInit(index,count);
    },
    swiperInit:function (activeIndex,count) {
        //初始化城市选择框
        this.mySwiper = new Swiper('#citySwiper',{
            slidesPerView :5,
            direction : 'vertical',
            initialSlide :activeIndex,
            freeMode : false,
            freeModeMomentumRatio : 1,
            freeModeMomentumVelocityRatio : 1,
            freeModeSticky : true,
            slidesOffsetBefore : adaptation(85),
            slidesOffsetAfter : adaptation(90),
            watchSlidesProgress : true,
            onProgress:function (swiper,progress) {
                var slides = swiper.slides;
                var len = slides.length;
                var index = Math.floor(progress*len);
                if(index<0)index=0;
                if(index>len-1) index=len-1;
                for (var i=0;i<len;i++){
                    if(i==index){
                        $(slides[index]).attr("class","activeIndex");
                    }else if(i==index-1 || i==index+1){
                        $(slides[i]).attr("class","activeFirst");
                    }else {
                        $(slides[i]).attr("class","");
                    }
                }
                if(index>=count){
                    ShoppingCart.mySwiper.slideTo(count-1, 1000, false);
                }
            }
        });
    },
    add:function () {
        var that = ShoppingCart;
        var index = that.mySwiper.activeIndex;
        var silde = that.mySwiper.slides[index];
        var id = parseInt($(silde).attr("id"));
        that.addTo(id-1);
    },
    addTo:function (id) {
        var that = ShoppingCart;
        var obj = that.cityData[id];
        var trigger = that.mapDom.find("div")[id];
        var myEvent = {x:0,y:0};
        myEvent.x = $(trigger).offset().left;
        myEvent.y = $(trigger).offset().top;
        $(trigger).css("background-image",ShoppingCart.url2);
        that.addCar($(trigger),$("<p>"+obj.cityName+"</p>"),myEvent,obj.id);
    },
    addCar:function (trigger,dom,event,id) {
        var that = this;
        if ("selected" != trigger.attr("data")) {
            trigger.attr("data", "selected");
            AddCart.add(dom, event,id,function (index) {
                that.recovery(index);
            });
        }else {
            alert("城市已选");
        }
    },
    recovery:function (index) {
        var item = this.mapDom.find("div");
        $(item[index-1]).css("background-image", this.url1);
        $(item[index-1]).attr("data", "");
    }
}
//选择平台页面
var Platform = {
    url1: "url(assets/img/platform/coil.png)",
    url2: "url(assets/img/platform/coilActive.png)",
    platformDom:null,
    platform_div_ul:null,
    liDom:null,
    platformData:[],
    init: function () {
        this.platformDom = $("#platform").show();
        this.platform_div_ul = this.platformDom.find("#platform_div_ul");

        //初始化平台
        this.platformData = [
            {
                id:0,
                name:'JINGDONG',
                name_CN:'京东',
                className:"logo1"
            },
            {
                id:1,
                name:'TMALL',
                name_CN:'天猫超市',
                className:"logo2"
            },
            {
                id:2,
                name:'YHD',
                name_CN:'1号店',
                className:"logo3"
            }
        ];

        var html = "";
        var arr = this.platformData;
        for (var i=0;i<arr.length;i++){
            html+="<li>"+
                "<div class='"+arr[i].className+"'></div>"+
                "<p>"+arr[i].name+"</p>"+
                "</li>";
        }

        this.platform_div_ul.html(html);
        this.liDom = this.platformDom.find("li");

        var that = this;
        $.each(this.liDom,function (i,o) {
            $(this).on('touchstart',function (event) {
                that.addCar($(this),event,i);
            })
        });
    },
    addTo:function (id) {
        var that = Platform;
        var obj = that.platformData[id];
        var trigger = that.liDom[id];
        var myEvent = {x:0,y:0};
        myEvent.x = $(trigger).offset().left;
        myEvent.y = $(trigger).offset().top;
        that.addCar(trigger,myEvent,id);
    },
    addCar:function (trigger,myEvent,i) {
        var that = this;
        if ("selected" != $(trigger).attr("data")) {
            $(trigger).css("background-image", that.url2);
            $(trigger).attr("data", "selected");
            AddCart.add($(trigger).find("div"), myEvent,i,function (index) {
                that.recovery(index);
            });
        }else {
            alert("平台已选");
        }
    },
    recovery: function (index) {
        $(this.liDom[index]).css("background-image", this.url1);
        $(this.liDom[index]).attr("data", "");
    }
}
//选择品牌页面
var Brand = {
    brandDom:null,
    swiperWrapper:null,
    url1: "url(assets/img/logo/coil.png)",
    url2: "url(assets/img/logo/coil-selected.png)",
    logoDom:null,
    brandArr:[],
    init:function () {
        this.brandDom = $("#brand").show();
        this.swiperWrapper = this.brandDom.find(".swiper-wrapper");
        var url = "assets/img/logo/";
        this.brandArr = [
            {
                id:0,
                name:"海飞丝",
                logo:url+'海飞丝.png'
            },
            {
                id:1,
                name:"潘婷",
                logo:url+'潘婷.png'
            },
            {
                id:2,
                name:"飘柔",
                logo:url+'飘柔.png'
            },
            {
                id:3,
                name:"VS",
                logo:url+'VS.png'
            },
            {
                id:4,
                name:"伊卡璐",
                logo:url+'伊卡璐.png'
            },
            {
                id:5,
                name:"SK2",
                logo:url+'SK2.png'
            },
            {
                id:6,
                name:"OLAY",
                logo:url+'OLAY.png'
            },
            {
                id:7,
                name:"舒肤佳",
                logo:url+'舒肤佳.png'
            },
            {
                id:8,
                name:"Febreze",
                logo:url+'Febreze.png'
            },
            {
                id:9,
                name:"ARIEL",
                logo:url+'ARIEL.png'
            },
            {
                id:10,
                name:"Tide",
                logo:url+'Tide.png'
            },
            {
                id:11,
                name:"sarasa",
                logo:url+'sarasa.png'
            },
            {
                id:12,
                name:"Oral-B",
                logo:url+'Oral-B.png'
            },
            {
                id:13,
                name:"博朗",
                logo:url+'博朗.png'
            },
            {
                id:14,
                name:"吉列",
                logo:url+'吉列.png'
            },
            {
                id:15,
                name:"佳洁士",
                logo:url+'佳洁士.png'
            },
            {
                id:16,
                name:"护舒宝",
                logo:url+'护舒宝.png'
            },
            {
                id:17,
                name:"Pampers",
                logo:url+'Pampers.png'
            }
        ];

        var len = this.brandArr.length;
        var html = "";
        for (var i=0;i<len;i++){
            if(i==0){
                html+="<div class='swiper-slide'><div class='centen'><ul>";
            }
            if(i==9){
                html+="</ul></div></div><div class='swiper-slide'><div class='centen'><ul>";
            }
            html+="<li><div class='logo'><div class='icon' style='background-image: url("+this.brandArr[i].logo+")'></div></div></li>";
        }
        html+="</ul></div></div>";
        this.swiperWrapper.html(html);

        this.logoDom = $(".logo");

        var that = this;
        $.each(this.logoDom,function (i,o) {
            $(this).on('touchstart',function () {
                that.addCar($(this),event,i);
            });
        })

        var mySwiper = new Swiper ('#brandSwiper', {
            // 如果需要分页器
            pagination: '.swiper-pagination',
            paginationClickable :true,
            // 如果需要前进后退按钮
            nextButton: '.swiper-button-next',
            prevButton: '.swiper-button-prev',
            iOSEdgeSwipeDetection : true,
            iOSEdgeSwipeThreshold : 50,
            touchRatio : 1,
            touchMoveStopPropagation : false,
            threshold : 200
        });
    },
    addTo:function (id) {
        var that = Brand;
        var obj = that.brandArr[id];
        var trigger = that.logoDom[id];
        var myEvent = {x:0,y:0};
        myEvent.x = $(trigger).offset().left;
        myEvent.y = $(trigger).offset().top;
        that.addCar(trigger,myEvent,id);
    },
    addCar:function (trigger,myEvent,i) {
        var that = this;
        if ("selected" != $(trigger).attr("data")) {
            $(trigger).css("background-image", that.url2);
            $(trigger).attr("data", "selected");
            AddCart.add($(trigger).find("div"), myEvent,i,function (index) {
                that.recovery(index);
            });
        }else {
            alert("品牌已选");
        }
    },
    recovery: function (index) {
        $(this.logoDom[index]).css("background-image", this.url1);
        $(this.logoDom[index]).attr("data", "");
    }
}
//录音对象
var recording = {
    url1:"url(assets/img/voice.png)",
    url2:"url(assets/img/recording.png)",
    recording:"false",
    voiceDom:null,
    switchCondition:0,
    init:function () {
        this.voiceDom = $("#voice");
        var that = this;

        //初始化讯飞录音
        Ios.xf_build(function (result) {
            //alert(result);
        },that.resultSuccess,that.resultFail);

        this.voiceDom.on('touchstart',function () {
            that.recording = that.voiceDom.attr("data");
            if (that.recording=="true"){
                that.endRecord();
            }else {
                that.startRecord();
            }
        });

    },
    startRecord:function () {
        this.voiceDom.css("background-image",this.url2);
        this.voiceDom.attr("data","true");
        Ios.xf_start(function (result) {
            //alert(result);
        })
    },
    endRecord:function () {
        this.voiceDom.css("background-image",this.url1);
        this.voiceDom.attr("data","false");
        Ios.xf_stop(function (result) {
            //alert(result);
        })
    },
    resultSuccess:function (result) {
        var index = recording.switchCondition;
        if(index==2){
            //选择平台
            var arr = Platform.platformData;
            var index = arr.length;
            $.each(arr,function (i,o) {
                if(o.name_CN==result){
                    index = i;
                    return;
                }
            });
            if(index<arr.length){
                // $(Platform.liDom[index]).trigger('click');
                Platform.addTo(index);
            }else {
                alert("对不起，当前只能选择平台");
            }
        }else if(index==3){
            //选择城市
            var arr = ShoppingCart.cityData;
            var index = arr.length;
            $.each(arr,function (i,o) {
                if(o.cityName_CN==result){
                    index = i;
                    return;
                }
            });
            if(!arr[index].data){
                alert("该城市暂无数据");
                return;
            }
            if(index<arr.length){
                ShoppingCart.addTo(index);
            }else {
                alert("对不起，当前只能选择城市");
            }
        }else if(index==4){
            //选择品牌
            var arr = Brand.brandArr;
            var index = arr.length;
            $.each(arr,function (i,o) {
                if(o.name==result){
                    index = i;
                    return;
                }
            });
            if(index<arr.length){
                // $(Brand.logoDom[index]).trigger('click');
                Brand.addTo(index);
            }else {
                alert("对不起，当前只能选择品牌");
            }
        }else {
            alert("系统异常");
        }
    },
    resultFail:function (result) {
        alert("匹配失败");
    }
}
//选中元素对象
var AddCart = {
    startX:adaptation(750),
    startY:adaptation(920),
    offsetX:adaptation(60),
    offsetY:adaptation(860),
    count:0,
    arr:[],
    add:function (dom,event,index,fun) {
        var liDom = $("<li></li>");
        var x = 0;
        var y = 0;
        if(typeof (event.type) ==='undefined'){
            x = event.x;
            y = event.y;
        }else {
            x = $(event.target).offset().left;
            y = $(event.target).offset().top;
        }

        var newDom = dom.clone();
        liDom.append(newDom);
        liDom.attr("class","addCar");
        liDom.attr("id","add"+this.count);
        liDom.css({left:x+"px",top:y+"px"});
        liDom.appendTo("body");
        liDom[0].index = index

        if(this.count>=9){
            $("#anchor").css({left:this.startX+(this.count-9)*this.offsetX+"px",top:this.offsetY});
        }else {
            $("#anchor").css({left:this.startX+this.count*this.offsetX+"px",top:this.startY});
            $("#ok").css({left:this.startX+(this.count+1)*this.offsetX+"px",display:"block"});
        }

        var that = this;
        $("#ok").on('touchstart',function () {
            $(this).css("display","none");
            that.deleteAll();
        })

        this.arr.push(liDom);

        this.shoppingCart("#add"+this.count,function (index) {
            fun(index);
        });

        this.count++;

    },
    shoppingCart:function (domName,fun) {
        var that = this;
        var bool = new Parabola({
            el: domName,
            targetEl:"#anchor",
            curvature: 0.004,
            duration: 800,
            autostart:true,
            callback: function () {
                $(domName).on('touchstart',function () {
                    $(this).remove();
                    //回调
                    fun(this.index);
                    that.count--;
                    that.sort();
                });
            },
            stepCallback: function (x, y) {

            }
        });
    },
    deleteAll:function () {
        var arr = this.arr;
        for(var i=0;i<arr.length;i++){
            arr[i].remove();
        }
        this.count=0;
    },
    sort:function () {
        var that = this;
        $.each($(".addCar"),function (i,o) {
            if(i>=9){
                $(this).css({left:that.startX+(i-9)*that.offsetX+"px",top:that.offsetY});
            }else {
                $(this).css({left:that.startX+i*that.offsetX+"px",top:that.startY});
            }
            $(this).attr("id","#add"+i);
        });
        if(this.count<=9){
            $("#ok").css({left:that.startX+(that.count)*that.offsetX+"px",display:"block"});
        }
    }
}
//获取当前时间，生成年、月、日数组
function getTime(start,end,type) {
    var date = new Date();
    var current = 0;
    var arr = [];
    if(type=='year'){
        if(end==0){
            current = date.getFullYear();
        }else {
            current = end;
        }
    }else if(type=='mouth'){
        current = end
    }else if(type=='day'){
        current = end
    }
    for (var i=start;i<=current;i++ ){
        if(type=='mouth'){
            arr.push(ChartPag.Letter[i]);
        }else if(type=='day'){
            if(i<10){
                arr.push('0'+i);
            }else {
                arr.push(i);
            }
        }else {
            arr.unshift(i);
        }
    }
    return arr;
}
//获取当前月的最大天数
function getDaysInMonth(year,month){
    month = parseInt(month,10)+1;
    var temp = new Date(year,month,0);
    return temp.getDate();
}
